1907D - Jumping Through Segments - CodeForces Solution


binary search constructive algorithms *1400

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
//#include<algorithm>
//#include<queue>
#define ll long long
#define QMax ((1<<22)-1)
#define rep(a, b) for (register int a = 0; a < b; a++)
#define rep2(a,c,b) for(ll a=c;a<b;a++)
#define myout(a) rep(i,a.size()) cout<<a[i]<<" ";cout<<"\n";
#define myout2(a) rep(i,a.size()){ rep(j,a[i].size())cout<<a[i][j]<<" ";cout<<"\n";}
#define mmin(a,b) (a>b? b:a)
#define mmax(a,b) (a<b? b:a)
//#define INF 1000000000

#include<algorithm>


using namespace std;

#define INF 1<<30

struct P2 {
    
    ll l,r;
};
vector<P2> A;
ll n;

bool chk(ll k) {

    P2 x = { 0,0 };

    rep(i, n) {
        ll s = x.l - k, e = x.r + k;
        x.l = mmax(s, A[i].l);
        x.r= mmin(e, A[i].r);
        if (x.l > x.r) return false;
    }
    return true;

}
int main()
{
  // freopen("a.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll TT; cin >> TT;

    while (TT--) {
        cin >> n;
        A.resize(n);
        rep(i, n) cin >> A[i].l >> A[i].r;



        ll ub = INF, lb = -1;
        ll mid;

        while (ub > lb + 1) {
            mid = (ub + lb )/ 2;
            if (chk(mid))
                ub = mid;
            else
                lb = mid;
        }
        ll ans = ub;

        cout << ans << "\n";
    }

    

}


Comments

Submit
0 Comments
More Questions

377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array